﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;




namespace App.SysOption
{
    public partial class frmUserManage : Form
    {
        public frmUserManage()
        {
            InitializeComponent();
            panlist.BringToFront();
            Tusers();
        }

        Entities db = new Entities();

        T_Stuff m_Stuff=null;


        private bool IsQuery = true;
        private bool bIsAdd = false;

        public int m_stID { get; set; }
      
        public int StuffID
        {
            set
            {
                var list = db.T_Stuff.Where(a => a.stID == value);
                foreach (T_Stuff stuff in list)
                {
                    m_Stuff = stuff;
                    break;
                }
                if (m_Stuff == null) return;


                string rid=db.T_Role.Where(a=>a.jsID==m_Stuff.roleID).Select(a=>a.roleName).FirstOrDefault();

                string position = db.T_PositionDesc.Where(a => a.id == m_Stuff.sPosition).Select(a => a.positionDesc).FirstOrDefault();

                txtName.Text = m_Stuff.name;
                txtNameEn.Text = m_Stuff.nameEn;
                txtNative.Text = m_Stuff.native;
                txtUsername.Text = m_Stuff.username;
                txtPwd.Text = m_Stuff.password;
                txtRole.Text = rid;
                txtSex.Text = m_Stuff.sex;
                txtBirth.Text = m_Stuff.birth.ToString();
                txtCardID.Text = m_Stuff.cardID;
                txtDepartment.Text = m_Stuff.department;
                txtGroup.Text = m_Stuff.sGroup;
                txtPosition.Text = position;
                txtUserID.Text = m_Stuff.userID;
                txtCampany.Text = m_Stuff.company;
                txtLanguageLevel.Text = m_Stuff.languageLevel;
                txtDegree.Text = m_Stuff.degree;
                txtMajor.Text = m_Stuff.major;
                txtGraduateFrom.Text = m_Stuff.graduateFrom;
                txtPolity.Text = m_Stuff.polity;
                txtisMarried.Text = m_Stuff.isMarried;
                txtCamputerLevel.Text = m_Stuff.computerLevel;
                txtPhone.Text = m_Stuff.phone;
                txtFax.Text = m_Stuff.fax;
                txtMobile.Text = m_Stuff.mobile;
                txtEmail.Text = m_Stuff.email;
                txtEmergencyContact.Text = m_Stuff.emergencyContact;
                txtecPhone.Text = m_Stuff.ecPhone;
                txtHomeAddress.Text = m_Stuff.homeAddress;
                txtnowAddress.Text = m_Stuff.nowAddress;
                txtEnterDate.ValueText = m_Stuff.enterDate;
                txtContractTerm.Text = m_Stuff.contractTerm;
                txtState.Text = m_Stuff.state;
                txtResume.Text = m_Stuff.resume;
                txtHobby.Text = m_Stuff.hobby;
                txtCultivate.Text = m_Stuff.cultivate;
                txtFamily.Text = m_Stuff.family;
                txtselfEvaluate.Text = m_Stuff.selfEvaluate;
                txtRemark.Text = m_Stuff.remark;
            }
        }

        //设置界面控制方面的方法
        //private void setWindowsControl()
        //{
        //    ArrayList listAdd = new ArrayList();
        //    listAdd.Add(new ControlEnable(this.btnAdd, false, this.btnAdd.GetType()));
        //    listAdd.Add(new ControlEnable(this.btnDel, false, this.btnDel.GetType()));
        //    listAdd.Add(new ControlEnable(this.btnEdit, false, this.btnEdit.GetType()));
        //    listAdd.Add(new ControlEnable(this.btnFirst, false, this.btnFirst.GetType()));
        //    listAdd.Add(new ControlEnable(this.btnPre, false, this.btnPre.GetType()));
        //    listAdd.Add(new ControlEnable(this.btnNext, false, this.btnNext.GetType()));
        //    listAdd.Add(new ControlEnable(this.btnLast, false, this.btnLast.GetType()));
        //    //listAdd.Add(new ControlEnable(this.btnQry, false, this.btnQry.GetType()));
        //    //listAdd.Add(new ControlEnable(this.btnDetail, false, this.btnDetail.GetType()));
        //    //listAdd.Add(new ControlEnable(this.btnExp, false, this.btnExp.GetType()));
        //    //listAdd.Add(new ControlEnable(this.btnCopy, false, this.btnCopy.GetType()));

        //    listAdd.Add(new ControlEnable(this.btnSave, true, this.btnSave.GetType()));
        //    listAdd.Add(new ControlEnable(this.btnCancel, true, this.btnCancel.GetType()));
        //    this.btnAdd.Tag = listAdd;

        //    ArrayList listEdit = new ArrayList();
        //    listEdit.Add(new ControlEnable(this.btnEdit, false, this.btnEdit.GetType()));
        //    listEdit.Add(new ControlEnable(this.btnFirst, false, this.btnFirst.GetType()));
        //    listEdit.Add(new ControlEnable(this.btnPre, false, this.btnPre.GetType()));
        //    listEdit.Add(new ControlEnable(this.btnNext, false, this.btnNext.GetType()));
        //    listEdit.Add(new ControlEnable(this.btnLast, false, this.btnLast.GetType()));
        //    //listEdit.Add(new ControlEnable(this.btnQry, false, this.btnQry.GetType()));
        //    //listEdit.Add(new ControlEnable(this.btnDetail, false, this.btnDetail.GetType()));
        //    //listEdit.Add(new ControlEnable(this.btnExp, false, this.btnExp.GetType()));
        //    //listEdit.Add(new ControlEnable(this.btnCopy, false, this.btnCopy.GetType()));
        //    listEdit.Add(new ControlEnable(this.btnAdd, false, this.btnAdd.GetType()));
        //    listEdit.Add(new ControlEnable(this.btnDel, false, this.btnDel.GetType()));

        //    listEdit.Add(new ControlEnable(this.btnSave, true, this.btnSave.GetType()));
        //    listEdit.Add(new ControlEnable(this.btnCancel, true, this.btnCancel.GetType()));
        //    this.btnEdit.Tag = listEdit;

        //    ArrayList listDel = new ArrayList();
        //    listDel.Add(new ControlEnable(this.btnFirst, false, this.btnFirst.GetType()));
        //    listDel.Add(new ControlEnable(this.btnPre, false, this.btnPre.GetType()));
        //    listDel.Add(new ControlEnable(this.btnNext, false, this.btnNext.GetType()));
        //    listDel.Add(new ControlEnable(this.btnLast, false, this.btnLast.GetType()));
        //    //listDel.Add(new ControlEnable(this.btnQry, false, this.btnQry.GetType()));
        //    //listDel.Add(new ControlEnable(this.btnDetail, false, this.btnDetail.GetType()));
        //    //listDel.Add(new ControlEnable(this.btnExp, false, this.btnExp.GetType()));
        //    listDel.Add(new ControlEnable(this.btnAdd, false, this.btnAdd.GetType()));
        //    listDel.Add(new ControlEnable(this.btnEdit, false, this.btnEdit.GetType()));

        //    listDel.Add(new ControlEnable(this.btnSave, true, this.btnSave.GetType()));
        //    listDel.Add(new ControlEnable(this.btnCancel, true, this.btnCancel.GetType()));
        //    this.btnDel.Tag = listDel;
        //    //this.btnCopy.Tag = listDel;

        //    ArrayList listSave = new ArrayList();
        //    listSave.Add(new ControlEnable(this.btnAdd, true, this.btnAdd.GetType()));
        //    listSave.Add(new ControlEnable(this.btnDel, true, this.btnDel.GetType()));
        //    listSave.Add(new ControlEnable(this.btnEdit, true, this.btnEdit.GetType()));
        //    listSave.Add(new ControlEnable(this.btnFirst, true, this.btnFirst.GetType()));
        //    listSave.Add(new ControlEnable(this.btnPre, true, this.btnPre.GetType()));
        //    listSave.Add(new ControlEnable(this.btnNext, true, this.btnNext.GetType()));
        //    listSave.Add(new ControlEnable(this.btnLast, true, this.btnLast.GetType()));
        //    //listSave.Add(new ControlEnable(this.btnQry, true, this.btnQry.GetType()));
        //    //listSave.Add(new ControlEnable(this.btnDetail, true, this.btnDetail.GetType()));
        //    //listSave.Add(new ControlEnable(this.btnExp, true, this.btnExp.GetType()));
        //    //listSave.Add(new ControlEnable(this.btnCopy, true, this.btnCopy.GetType()));

        //    listSave.Add(new ControlEnable(this.btnSave, false, this.btnSave.GetType()));
        //    listSave.Add(new ControlEnable(this.btnCancel, false, this.btnCancel.GetType()));
        //    this.btnSave.Tag = listSave;
        //    this.btnCancel.Tag = listSave;
        //}

        

        //绑定用户表
        private void Tusers()
        {
            var userlist = from a in db.T_Stuff
                           from b in db.T_Role
                           from c in db.T_PositionDesc
                           where
                               a.roleID == b.jsID && a.sPosition == c.id
                           select new {a.stID,a.name,a.nameEn,a.department,a.sGroup,b.roleName,c.positionDesc,a.mobile,
                           a.email,a.phone,a.username};
            bs.DataSource = userlist.ToList();
            nav.BindingSource = bs;

            var departlist=db.T_DepartmentDesc.ToList();
            txtDepartment.DataSource = departlist;
            txtDepartment.DisplayMember = "depart";
            txtDepartment.ValueMember = "depart";

            var grouplist = db.T_GroupDesc.ToList();
            txtGroup.DataSource = grouplist;
            txtGroup.DisplayMember = "GroupName";
            txtGroup.ValueMember = "GroupName";

            var rolelist = db.T_Role.ToList();
            txtRole.DataSource = rolelist;
            txtRole.DisplayMember = "roleName";
            txtRole.ValueMember = "jsID";

            var positionlist = db.T_PositionDesc.ToList();
            txtPosition.DataSource = positionlist;
            txtPosition.DisplayMember = "positionDesc";
            txtPosition.ValueMember = "id";
        }

        private void frmUserManage_Load(object sender, EventArgs e)
        {
            
            
        }

        private void btn1_Click(object sender, EventArgs e)
        {
            GetGridViewID();
            if (this.IsQuery)
            {
                this.pandetail.BringToFront();
                this.btn1.Text = "概要";
                this.IsQuery = false;
            }
            else
            {
                this.panlist.BringToFront();
                this.btn1.Text = "明细";
                this.IsQuery = true;
            }
        }


        private void gv1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {

            try
            {
                this.StuffID = Convert.ToInt32(gv1.Rows[e.RowIndex].Cells["colID"].Value);
                this.pandetail.BringToFront();
                this.btn1.Text = "概要";
                this.IsQuery = false;
            }
            catch
            {
                Msg.ShowError("请选择表里的数据！");
            }
        }

        //private void gv1_CellClick(object sender, DataGridViewCellEventArgs e)
        //{

        //}

        private void GetGridViewID()
        {
            if (gv1.RowCount > 0)
            {
                int index = gv1.CurrentRow.Index;    //取得选中行的索引  
                this.m_stID = Convert.ToInt32(gv1.Rows[index].Cells["colID"].Value);
                this.StuffID = Convert.ToInt32(gv1.Rows[index].Cells["colID"].Value);
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            //btnFirst.Enabled = false;
            //btnPre.Enabled = false;
            //btnNext.Enabled = false;
            //btnLast.Enabled = false;
            //btnAdd.Enabled = false;
            //btnDel.Enabled = false;
            //btnEdit.Enabled = false;
            //btnSave.Enabled = true;
            //btnCancel.Enabled = true;

            this.StuffID = 0;
            m_Stuff = null;
            this.pandetail.BringToFront();
            this.btn1.Text = "概要";
            this.IsQuery = false;

            this.bIsAdd = true;

            txtName.Text = "";
            txtNameEn.Text = "";
            txtNative.Text = "";
            txtUsername.Text = "";
            txtPwd.Text = "";
            txtRole.Text = "";
            txtSex.Text = "";
            txtBirth.Text = "";
            txtCardID.Text = "";
            txtDepartment.Text = "";
            txtGroup.Text = "";
            txtPosition.Text = "";
            txtUserID.Text = "";
            txtCampany.Text = "";
            txtLanguageLevel.Text = "";
            txtDegree.Text = "";
            txtMajor.Text = "";
            txtGraduateFrom.Text = "";
            txtPolity.Text = "";
            txtisMarried.Text = "";
            txtCamputerLevel.Text = "";
            txtPhone.Text = "";
            txtFax.Text = "";
            txtMobile.Text = "";
            txtEmail.Text = "";
            txtEmergencyContact.Text = "";
            txtecPhone.Text = "";
            txtHomeAddress.Text = "";
            txtnowAddress.Text = "";
            txtEnterDate.ValueText = "";
            txtContractTerm.Text = "";
            txtState.Text = "";
            txtResume.Text = "";
            txtHobby.Text = "";
            txtCultivate.Text = "";
            txtFamily.Text = "";
            txtselfEvaluate.Text = "";
            txtRemark.Text = "";
        }

        private void SaveAdd()
        {
            m_Stuff = new T_Stuff();

            if (db.T_Stuff.Count() <= 0) m_Stuff.stID = 1;
            else m_Stuff.stID = db.T_Stuff.OrderByDescending(a => a.stID).First().stID + 1;
            m_Stuff.name = txtName.Text;
            m_Stuff.nameEn = txtNameEn.Text;
            m_Stuff.native = txtNative.Text;
            m_Stuff.username = txtUsername.Text;
            m_Stuff.password = txtPwd.Text;
            m_Stuff.roleID = db.T_Role.Where(a => a.roleName == txtRole.Text).Select(a => a.jsID).FirstOrDefault();
            m_Stuff.sex = txtSex.Text;
            if (!string.IsNullOrEmpty(txtBirth.Text)) m_Stuff.birth = Convert.ToDateTime(txtBirth.Text);
            m_Stuff.cardID = txtCardID.Text;
            m_Stuff.department = txtDepartment.Text;
            m_Stuff.sGroup = txtGroup.Text;
            m_Stuff.sPosition = db.T_PositionDesc.Where(a=>a.positionDesc==txtPosition.Text).Select(a=>a.id).FirstOrDefault();
            m_Stuff.userID = txtUserID.Text;
            m_Stuff.company = txtCampany.Text;
            m_Stuff.languageLevel = txtLanguageLevel.Text;
            m_Stuff.degree = txtDegree.Text;
            m_Stuff.major = txtMajor.Text;
            m_Stuff.graduateFrom = txtGraduateFrom.Text;
            m_Stuff.polity = txtPolity.Text;
            m_Stuff.isMarried = txtisMarried.Text;
            m_Stuff.computerLevel = txtCamputerLevel.Text;
            m_Stuff.phone = txtPhone.Text;
            m_Stuff.fax = txtFax.Text;
            m_Stuff.mobile = txtMobile.Text;
            m_Stuff.email = txtEmail.Text;
            m_Stuff.emergencyContact = txtEmergencyContact.Text;
            m_Stuff.ecPhone = txtecPhone.Text;
            m_Stuff.homeAddress = txtHomeAddress.Text;
            m_Stuff.nowAddress = txtnowAddress.Text;
            m_Stuff.enterDate = txtEnterDate.ValueText;
            m_Stuff.contractTerm = txtContractTerm.Text;
            m_Stuff.state = txtState.Text;
            m_Stuff.resume = txtResume.Text;
            m_Stuff.hobby = txtHobby.Text;
            m_Stuff.cultivate = txtCultivate.Text;
            m_Stuff.family = txtFamily.Text;
            m_Stuff.selfEvaluate = txtselfEvaluate.Text;
            m_Stuff.remark = txtRemark.Text;

            db.T_Stuff.Add(m_Stuff);
            int i = db.SaveChanges();
            Msg.ShowInfo(string.Format("已保存{0}条用户信息。", i));
            this.panlist.BringToFront();
            this.btn1.Text = "明细";
            this.IsQuery = true;
            
        }

        private void SaveModify()
        {
            if (m_Stuff.stID <= 0) return;
            m_Stuff = db.T_Stuff.Where(a => a.stID == m_Stuff.stID).First();

            m_Stuff.name = txtName.Text;
            m_Stuff.nameEn = txtNameEn.Text;
            m_Stuff.native = txtNative.Text;
            m_Stuff.username = txtUsername.Text;
            m_Stuff.password = txtPwd.Text;
            m_Stuff.roleID = db.T_Role.Where(a => a.roleName == txtRole.Text).Select(a => a.jsID).FirstOrDefault();
            m_Stuff.sex = txtSex.Text;
            m_Stuff.birth = Convert.ToDateTime(txtBirth.Text);
            m_Stuff.cardID = txtCardID.Text;
            m_Stuff.department = txtDepartment.Text;
            m_Stuff.sGroup = txtGroup.Text;
            m_Stuff.sPosition = db.T_PositionDesc.Where(a => a.positionDesc == txtPosition.Text).Select(a => a.id).FirstOrDefault();
            m_Stuff.userID = txtUserID.Text;
            m_Stuff.company = txtCampany.Text;
            m_Stuff.languageLevel = txtLanguageLevel.Text;
            m_Stuff.degree = txtDegree.Text;
            m_Stuff.major = txtMajor.Text;
            m_Stuff.graduateFrom = txtGraduateFrom.Text;
            m_Stuff.polity = txtPolity.Text;
            m_Stuff.isMarried = txtisMarried.Text;
            m_Stuff.computerLevel = txtCamputerLevel.Text;
            m_Stuff.phone = txtPhone.Text;
            m_Stuff.fax = txtFax.Text;
            m_Stuff.mobile = txtMobile.Text;
            m_Stuff.email = txtEmail.Text;
            m_Stuff.emergencyContact = txtEmergencyContact.Text;
            m_Stuff.ecPhone = txtecPhone.Text;
            m_Stuff.homeAddress = txtHomeAddress.Text;
            m_Stuff.nowAddress = txtnowAddress.Text;
            m_Stuff.enterDate = txtEnterDate.ValueText;
            m_Stuff.contractTerm = txtContractTerm.Text;
            m_Stuff.state = txtState.Text;
            m_Stuff.resume = txtResume.Text;
            m_Stuff.hobby = txtHobby.Text;
            m_Stuff.cultivate = txtCultivate.Text;
            m_Stuff.family = txtFamily.Text;
            m_Stuff.selfEvaluate = txtselfEvaluate.Text;
            m_Stuff.remark = txtRemark.Text;

            int i = db.SaveChanges();
            Msg.ShowInfo(string.Format("已保存{0}条用户信息。", i));
            this.panlist.BringToFront();
            this.btn1.Text = "明细";
            this.IsQuery = true;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            //btnSave.Enabled = false;
            //btnCancel.Enabled = false;
            //btn1.Enabled = true ;
            //btnFirst.Enabled = true;
            //btnPre.Enabled = true;
            //btnNext.Enabled = true;
            //btnLast.Enabled = true;
            //btnEdit.Enabled = true;
            //btnDel.Enabled = true;

            if (bIsAdd)
            {
                if (db.T_Stuff.Where(a => a.name == txtName.Text.Trim()).ToList().Count > 0)
                {
                    Msg.ShowError("用户名不能重复！");
                    txtName.Focus();
                    btnSave.Enabled = true;
                    btnCancel.Enabled = true;
                    return;
                }
            }
            else if (txtRole.Text.Trim() == string.Empty)
            {
                Msg.ShowError("权限不能为空！");
                txtRole.Focus();
                btnSave.Enabled = true;
                btnCancel.Enabled = true;
                return;
            }
            else if (txtPosition.Text.Trim() == string.Empty)
            {
                Msg.ShowError("职位不能为空！");
                txtPosition.Focus();
                btnSave.Enabled = true;
                btnCancel.Enabled = true;
                return;
            }

            if (Msg.ShowWarning("您确信保存用户信息吗？") == System.Windows.Forms.DialogResult.Yes)
            {
                if (m_Stuff == null)
                {
                    SaveAdd();
                    Tusers();
                }
                else
                {
                    SaveModify();
                    Tusers();
                }
            }
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            GetGridViewID();
            if (Msg.ShowWarning("您确信删除用户信息吗？") == System.Windows.Forms.DialogResult.Yes)
            {
                if (m_stID < 0)
                {
                    Msg.ShowError("请选择用户！");
                    return;
                }
                T_Stuff stuff = db.T_Stuff.First(a => a.stID == m_stID);
                db.T_Stuff.Remove(stuff);
                int i = db.SaveChanges();
                Msg.ShowInfo(string.Format("已删除{0}条用户信息。", i));
                Tusers();
            }
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            GetGridViewID();
            //btnFirst.Enabled = false;
            //btnPre.Enabled = false;
            //btnNext.Enabled = false;
            //btnLast.Enabled = false;
            //btnAdd.Enabled = false;
            //btnDel.Enabled = false;
            //btnEdit.Enabled = false;
            //btnSave.Enabled = true;
            //btnCancel.Enabled = true;

            this.pandetail.BringToFront();
            this.btn1.Text = "概要";
            this.IsQuery = false;

        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            if (Msg.ShowWarning("尚未保存，您确定要取消吗？") == System.Windows.Forms.DialogResult.Yes)
            {
                this.panlist.BringToFront();
                this.btn1.Text = "明细";
                this.IsQuery = true;
            }
        }
        
    }
}
